import './index.less';
import React from "react";
import {inject, observer} from "mobx-react/index";
import {Condition, getAdButtons} from './Condition';
import {WeaDialog, WeaTab, WeaRightMenu, WeaDropdown, WeaPopoverHrm, WeaHelpfulTip, WeaLocaleProvider} from 'ecCom';
import {WeaTableNew} from "comsMobx";
import {Button} from 'antd';

import BatchSubmit from '../../../asyncComps/BatchSubmit';
import { doBatchSubmit } from '../../../apis/list';
import ShareBatch from '../../../asyncComps/BatchShare';
import {toJS} from "mobx";

const WeaTable = WeaTableNew.WeaTable;
const { ButtonSelect } = WeaDropdown;
const getLabel = WeaLocaleProvider.getLabel;

@inject('wfCustomReportReqListStore')
@observer
export default class Main extends React.Component{

    constructor(props){
        super(props);
        this.state = {
            height : 510
        }
    }

    componentWillReceiveProps(nextProps) {
        if(this.props.modalVisible !== nextProps.modalVisible && nextProps.modalVisible) {
            const { wfCustomReportReqListStore = {}, dataParams = {} } = nextProps;
            wfCustomReportReqListStore.tableStore.setSelectedRowKeys([]);
            wfCustomReportReqListStore.updateOtherSearchParams(dataParams);
            wfCustomReportReqListStore.doSearch();
        }
    }

    componentDidMount(){
        const { wfCustomReportReqListStore = {} } = this.props;
        wfCustomReportReqListStore.loadCondition();
    }

    render(){
        const {height} = this.state;
        const { wfCustomReportReqListStore, modalVisible ,closerReqListModal} = this.props;
        const { tableStore,form,loading=false,showSearchAd,title= '',showModalObj} = wfCustomReportReqListStore;
        const formParams = form.getFormParams() || {};
        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@p4zkjr`}
                visible={modalVisible}
                title={title}
                icon="icon-coms-workflow"
                iconBgcolor='#0079DE'
                maskClosable={false}
                buttons={this.getFooter()}
                onCancel = {this.doCancel}
                style={{'width':1000,'height':510}}
                className={"wf-custom-report-request-list-dialog"}
            >
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@0mmz38`}
                    collectParams={{favname:title, favouritetype:2}}
                    datas={this.getRightMenu()}
                >
                    <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@37sbp0`} onSearch={()=>{wfCustomReportReqListStore.doSearch()}}
                            onSearchChange={v=>{wfCustomReportReqListStore.setFormFields({requestname:{value:v}})}}
                            searchType={['base','advanced']}
                            searchsBaseValue={formParams.requestname}
                            setShowSearchAd={bool=>{wfCustomReportReqListStore.setShowSearchAd(bool)}}
                            hideSearchAd={()=> wfCustomReportReqListStore.setShowSearchAd(false)}
                            showSearchAd = {showSearchAd}
                            buttonsAd={getAdButtons(wfCustomReportReqListStore, this.props && this.props.ecId || '')}
                            searchsAd={
                                <Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@chlv4c`} requestListStore={wfCustomReportReqListStore} />
                            }
                            loading = {loading}
                    />
                    <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@q501k3`} comsWeaTableStore={tableStore}
                              rowKey={(record,index)=>{
                                  return `${record.requestid}_${record.userid||""}`;
                              }}
                              scroll={{y: height-125}}
                              getColumns={this.getColumns}
                    />
                </WeaRightMenu>
                <ShareBatch ecId={`${this && this.props && this.props.ecId || ''}_ShareBatch@q5hnu0`}
                    visible={showModalObj.batchShare || false}
                    controlModal={v=>wfCustomReportReqListStore.controlShowModal("batchShare", v)}
                    ids={tableStore.selectedRowKeys}
                />
                <BatchSubmit ecId={`${this && this.props && this.props.ecId || ''}_BatchSubmit@urf69v`}
                    visible={showModalObj.batchSubmit || false}
                    onSubmit={this.batchSubmitEvent.bind(this)}
                    onCancel={this.batchSubmitCancel}
                />
                <WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@37cn66`}/>
            </WeaDialog>
        )
    }

    batchSubmitCancel = ()=>{this.props.wfCustomReportReqListStore.controlShowModal("batchSubmit", false)}
    getColumns = (columns) => {
        const columnsJS = toJS(columns);
        columnsJS.forEach(col => {
            if(col.dataIndex === "unoperators"){
                const title = (
                    <span>
                        <span title={col.title}>
                            { col.title }
                        </span>
                        {<WeaHelpfulTip ecId={`${this && this.props && this.props.ecId || ''}_WeaHelpfulTip@riaa6o@${col.dataIndex}`} style={{ marginLeft: 5 }} title={getLabel(516719,"可在【个性化设置】中设置未操作者的显示类型")} placement="top" />}
                    </span>
            )
                col.title = title;
            }
        });
        return columnsJS;
    }

    getRightMenu(){
        const { wfCustomReportReqListStore } = this.props;
        const { tableStore } = wfCustomReportReqListStore;
        let btns = [];
        btns.push({
            key: "search",
            icon: <i className='icon-workflow-Right-menu--search'/>,
            content: '搜索',
            onClick: key =>{
                wfCustomReportReqListStore.doSearch();
            }
        });
        btns.push({
            key: "colSet",
            icon: <i className='icon-workflow-Right-menu-Custom'/>,
            content: '显示列定制',
            onClick: key =>{
                tableStore.setColSetVisible(true);
                tableStore.tableColSet(true);
            }
        });
        return btns;
    }

    batchShareClick(selectedRowKeys){
        const { wfCustomReportReqListStore } = this.props;
        wfCustomReportReqListStore.controlShowModal("batchShare", true);
    }
    batchSubmitClick(){
        const { wfCustomReportReqListStore } = this.props;
        const { otherInfo } = wfCustomReportReqListStore;
        if(otherInfo.multisubmitnotinputsign == "1")     //弹窗输入签字意见
            wfCustomReportReqListStore.controlShowModal("batchSubmit", true);
        else
            this.batchSubmitEvent("");
    }

    batchSubmitEvent(remark){
        const { wfCustomReportReqListStore } = this.props;
        const { tableStore } = wfCustomReportReqListStore;
        wfCustomReportReqListStore.setLoading(true);
        doBatchSubmit(remark, tableStore.selectedRowKeys, []).then(data => {
            wfCustomReportReqListStore.setLoading(false);
            wfCustomReportReqListStore.reLoad();
        });
        wfCustomReportReqListStore.controlShowModal("batchSubmit", false);
    }

    getFooter () {
        const {closerReqListModal,wfCustomReportReqListStore} = this.props;
        const { tableStore } = wfCustomReportReqListStore;
        const selectedRowKeys = tableStore.selectedRowKeys;
        const btndatas = [
            {key:"batchShare", show:"批量共享", selected: true},
            {key:"batchSubmit", show:"批量提交"}
        ];
        let btns =[];
        const { ButtonSelect } = WeaDropdown;
        btns.push(<ButtonSelect ecId={`${this && this.props && this.props.ecId || ''}_ButtonSelect@mabqhj@batch`} datas={btndatas}
                                btnProps={{disabled:selectedRowKeys.length <= 0}}
                                dropdownProps={{trigger:['click']}}
                                btnOnClick={key =>{
                                    if(key === "batchShare")
                                        this.batchShareClick(selectedRowKeys);
                                    else if(key === "batchSubmit")
                                        this.batchSubmitClick();
                                }}
                                menuOnClick={(key, e) =>{
                                    wfCustomReportReqListStore.clickButttonDrop(key);
                                }}
        />);
        btns.push(
            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ygozj9@cancel`} type='glost' onClick={this.doCancel.bind(this)}>取消</Button>
        );
        return btns;
    }

    doCancel = () => {
        const {closerReqListModal,wfCustomReportReqListStore} = this.props;
        closerReqListModal && closerReqListModal();
        wfCustomReportReqListStore.setShowSearchAd(false)
        wfCustomReportReqListStore.clearFormFields();
    }

}